**********************************************
********* Código para la generación **********
********* de tablas de balance ***************

clear
set more off
	
*Mando a llamar la base con la información requerida:
use "$base_out/Modified_BaseContrasteInteres.dta"

*Traigo base que identifican missing en endline*

rename clavecct school_id

merge 1:1 school_id using "$basein/Datos adicionales/DWMS_missing_endline.dta", gen(_DMWS)

replace DMS_missing_endline=1 if DMS_missing_endline==. & treatgroup2!=""

merge 1:1 school_id using "$basein/Datos adicionales/PLANEA_missing_endline.dta", gen(_PLANEA)

	
merge 1:1 school_id using "$basein/Datos adicionales/secundarias.dta", gen(_secundaria)

keep if _secundaria==3

rename  school_id clavecct

replace PLANEA_missing_Endlile=0 if PLANEA_missing_Endlile==.

*Dejo solo escuelas primarias*

keep if secondary!=1

*****************************************+

eststo clear
	label var f_T_profe "Number of teachers "
	label var f_T_estu "Number of students "
	label var f_studteach_ratio "Student-teacher ratio"
	label var mathIV "Students in math achievement L-IV (\%)" 			
	label var mathI "Students in math achievement L-I (\%)" 		 			
	label var langI "Students in language achievement L-I (\%)" 		
	label var langIV "Students in language achievement L-IV (\%)" 
	label var Alta_marginacion "Marginalization"
	label var Urbanas "Urbanization"
	label var DMS_missing_endline "DWMS endline missing"
	label var PLANEA_missing_Endlile "PLANEA endline missing"
	

*******************************
******* Strat_id(estrato) *****
*******************************	

	 
	 ***************** GENERAL *******************
	 
	 eststo General:  my_ptest_pec_vs_pecplus mathIV mathI langIV langI Alta_marginacion Urbanas f_T_estu f_T_profe f_studteach_ratio DMS_missing_endline PLANEA_missing_Endlile, by(treatgroup2) clus_id(clavecct) strat_id(estrato)
	 

	 	 esttab General  using "$tables/EstrBalance_General.tex", label replace  nolines nogaps fragment ///
	 star(* 0.1 ** 0.05 *** 0.01)  ///
	 mlabels(none) nomtitles nonumbers noobs nodep collabels(none)  ///
     cells("mu_2(fmt(%9.2fc)) mu_3(fmt(%9.2fc) ) dif2(fmt(%9.2fc) star pvalue(d_p2))" "se_2(par) se_3(par) des_2(par)") 
	 
 	replace Alta_marginacion=Alta_marginacion*100
	replace Urbanas=Urbanas*100
	 foreach var in mathIV mathI langIV langI Alta_marginacion Urbanas f_T_estu f_T_profe f_studteach_ratio DMS_missing_endline PLANEA_missing_Endlile{
		
	 
	 
		reg `var'  TD1 TD2, nocons vce(cluster clavecct)
		sum `var' if e(sample)==1
		local tempm=string(r(mean), "%9.2gc")
		file open newfile using "$tables/`var'_General_mean.tex", write replace
		file write newfile "`tempm'"
		file close newfile
		
		sum `var' if TD1==1 & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_General_Control.tex", write replace
		file write newfile "`tempm'"
		file close newfile
		sum `var' if TD2==1 & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_General_PecPlus.tex", write replace
		file write newfile "`tempm'"
		file close newfile

		
		
		reghdfe `var'  TD1 TD2 if treatgroup!="Control" & state!=10,  vce(cluster clavecct) absorb(i.estrato)
		sum `var' if (TD1==1 | TD2==1) & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_General_Dif2.tex", write replace
		file write newfile "`tempm'"
		file close newfile
		
		
		}


	tab  treatgroup2
local tempm=string(r(N), "%9.2gc")
file open newfile using "$tables/N_TotalEscuelas.tex", write replace
file write newfile "`tempm'"
file close newfile

		
tab treatgroup2 if treatgroup2=="PEC"
local tempm=string(r(N), "%9.2gc")
file open newfile using "$tables/N_TotalEscuelas_Control.tex", write replace
file write newfile "`tempm'"
file close newfile

tab treatgroup2 if treatgroup2=="PEC Plus"
local tempm=string(r(N), "%9.2gc")
file open newfile using "$tables/N_TotalEscuelas_Treatment.tex", write replace
file write newfile "`tempm'"
file close newfile

